package at.ac.univie.zsu.aguataplan.ui;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import at.ac.univie.zsu.aguataplan.domain.Club;
import at.ac.univie.zsu.aguataplan.domain.Team;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;
import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.property.I18nProperties;
import at.ac.univie.zsu.aguataplan.service.ClubManager;
import at.ac.univie.zsu.aguataplan.service.TeamManager;
import at.ac.univie.zsu.aguataplan.ui.template.IDialogListEntry;
import at.ac.univie.zsu.aguataplan.ui.template.ListEntryPanel;

/**
 * @author gerry
 * 
 */
public class DialogTeam extends JDialog implements UIConstants {

	private static final long serialVersionUID = 1L;

	private JFrame parent;
	private GeneralProperties generalProperties;
	private I18nProperties i18nProp;

	private TeamManager teamManager;
	private ClubManager clubManager;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public DialogTeam(MainWindow parent, GeneralProperties generalProperties) {
		super(parent, true);

		this.parent = parent;
		this.generalProperties = generalProperties;
		this.i18nProp = generalProperties.getI18nProperties();
		this.teamManager = parent.getServices().getTeamManager();
		this.clubManager = parent.getServices().getClubManager();

		this.setLayout(null);
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setLocationByPlatform(true);
		setSize(SPACING + PANEL_SIZE_X_ENTRY + SPACING + SPACING, SPACING + PANEL_SIZE_Y_ENTRY + SPACING
				+ DIALOG_SIZE_Y_HEADER);
		setTitle(i18nProp.getString(TEAM_HEADER));
		setResizable(false);

		addEntries();

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	private void addEntries() {
		// team panel
		ListEntryPanel teamPanel = new ListEntryPanel(this, generalProperties, new IDialogListEntry() {

			@Override
			public List<String> select(Object obj) {
				Team team = (Team) obj;
				List<String> list = new ArrayList<String>();
				list.add(new String(team.getName()));
				return list;
			}

			@Override
			public Object merge(Object obj, List<String> list, List<Object> combos, List<Date> dates) {
				Team team = (Team) obj;

				team.setName(list.get(0));
				team.setRefClub((Club) combos.get(0));

				Team t = null;

				try {
					t = teamManager.mergeTeam(team);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogTeam.this, i18nProp.getString(EX_TEAM_SAVE_DESC),
							i18nProp.getString(EX_TEAM), JOptionPane.ERROR_MESSAGE);
				}

				return t;
			}

			@Override
			public List<?> initText() {
				List<Team> teams = null;
				try {
					teams = teamManager.getTeamList();
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogTeam.this, i18nProp.getString(EX_TEAM_LIST_DESC),
							i18nProp.getString(EX_TEAM), JOptionPane.ERROR_MESSAGE);
				}
				return teams;
			}

			@Override
			public Object delete(Object obj) {
				Team team = (Team) obj;

				try {
					teamManager.deleteTeam(team.getId());
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogTeam.this, i18nProp.getString(EX_TEAM_DELETE_DESC),
							i18nProp.getString(EX_TEAM), JOptionPane.ERROR_MESSAGE);
					return null;
				}

				return team;
			}

			@Override
			public Object add(List<String> list, List<Object> combos, List<Date> dates) {
				Club club;
				club = (Club) combos.get(0);
				if (club == null) {
					return null;
				}

				Team team = null;

				try {
					team = teamManager.addTeam(list.get(0), club);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogTeam.this, i18nProp.getString(EX_TEAM_ADD_DESC),
							i18nProp.getString(EX_TEAM), JOptionPane.ERROR_MESSAGE);
				}

				return team;
			}

			@Override
			public List<List<?>> initCombo() {
				List<List<?>> l = null;

				List<Club> clubs;
				try {
					clubs = clubManager.getClubList();
					l = new ArrayList<List<?>>();
					l.add(clubs);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogTeam.this, i18nProp.getString(EX_TEAM_LIST_DESC),
							i18nProp.getString(EX_TEAM), JOptionPane.ERROR_MESSAGE);
				}
				return l;
			}

			@Override
			public List<Integer> selectCombo(Object obj) {
				Team team = (Team) obj;

				List<Integer> list = null;

				try {
					Integer pos = clubManager.getClubListPosition(team.getRefClub().getId());
					list = new ArrayList<Integer>();
					list.add(pos);

				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogTeam.this, i18nProp.getString(EX_TEAM_LIST_DESC),
							i18nProp.getString(EX_TEAM), JOptionPane.ERROR_MESSAGE);
				}

				return list;
			}

			@Override
			public List<Date> initDate() {
				return null;
			}

			@Override
			public List<Date> selectDate(Object obj) {
				return null;
			}
		});
		List<String> listTeam = new ArrayList<String>();
		listTeam.add(new String(i18nProp.getString(TEAM_NAME)));
		List<String> listComboTeam = new ArrayList<String>();
		listComboTeam.add(new String(i18nProp.getString(TEAM_CLUB)));
		teamPanel.init(i18nProp.getString(TEAM_BORDER), PANEL_SIZE_X_ENTRY, PANEL_SIZE_Y_ENTRY, listTeam,
				listComboTeam, new ArrayList<String>(), new ArrayList<String>(), new ArrayList<ActionListener>());

		// add to dialog
		teamPanel.setLocation(SPACING, SPACING);
		this.add(teamPanel);

		// finish init
		teamPanel.finishInit();

	}
}
